<?php


/**
 * Converts an Array into an XML string
 * 
 * Although it doesn't work with tag attributes, this function solves a problem
 * encountered in many Array2XML functions, which is dealing with multiple tags
 * on the same level that have the same name.
 * 
 * This way you can convert:
 * 
 * $array["root"]["child"][0] = "element1";
 * $array["root"]["child"][1] = "element2";
 * $array["root"]["child"][2] = "element3";
 * $array["root"]["child"][3] = "element4";
 * 
 * Into:
 * 
 * <root>
 *   <child>element1</child>
 *   <child>element2</child>
 *   <child>element3</child>
 *   <child>element4</child>
 * </root>
 * 
 * @param Array
 * @param string
 */
function Services_Plesk_array_to_xml($array, $parent = null) {
    
    /* Navigates through all the elements of the array */
    foreach ($array as $key => $value) {
        
        /**
         * If the element has a numeric index it means that the tag name must be
         * the key of the array that contains it. Example:
         * 
         * $array["root"]["child"][0] = "element1";
         * $array["root"]["child"][1] = "element2";
         * $array["root"]["child"][2] = "element3";
         * $array["root"]["child"][3] = "element4";
         * 
         * In this case all elements will have the tag <child>.
         * 
         * This tag will be closed in the end of the 'foreach' loop.
         * 
         */
        if (is_int($key)) {
            
            $xml .= "<$parent>";
            
        }
        
        /* If the current value is an array, a recursive call will be made */
        if (is_array($value)) {
            
            /**
             * If the child elements of the array are ordered with a numeric index,
             * the recursive call must be made passing the current index, which will
             * be used as tag name for all of them.
             */
            if (array_key_exists(0, $value)) {
                
                $xml .= Services_Plesk_array_to_xml($value, $key);
                
            } else {
                
                
                if (is_int($key)) {
                    
                    /* Make the recursive call ignoring the current index because it's numeric */                    
                    $xml .= Services_Plesk_array_to_xml($value);
                    
                } else {
                    
                    /* Create the tag and make the recursive call */
                    $xml .= "<$key>" . Services_Plesk_array_to_xml($value) . "</$key>";
                    
                }
            }
            
        } else if (is_null($value)) {
            
            /* If you have a null element in your array, the tag must be empty */
            $xml .= "<$key/>";
            
        } else {
            
            
            if (is_int($key)) {
                
                /**
                 * In this case the tag doesn't need to be printed, it has already
                 * been opened in the beggining of the foreach loop and will be
                 * closed in the end of the loop.
                 */                
                $xml .= "$value";
                
            } else {
                
                /* Inserts the tag name and its value */
                $xml .= "<$key>$value</$key>";
			}
        }
        
        if (is_int($key)) {
            
            $xml .= "</$parent>";
            		
        }
                
    }
   
    return $xml;
    
}


?>